package com.villaday.idf.activity;

import android.content.Intent;
import android.os.Handler;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.AppContext;
import com.villaday.idf.R;
import com.villaday.idf.adapter2.MinSuSearchHeadAdapter;
import com.villaday.idf.adapter2.SearchForJiuDian2VAdapter;
import com.villaday.idf.adapter2.SearchForMoreHotHouse2VAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.BournSearchhistory;
import com.villaday.idf.model.H;
import com.villaday.idf.model.Resources_filtersEntity;
import com.villaday.idf.model.Search_filtersEntity;
import com.villaday.idf.model2.BieShuSearch;
import com.villaday.idf.model2.MinSuSearch;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.DbBSHUtils;
import com.villaday.idf.util.DbHUtils;
import com.villaday.idf.util.DbVUtils;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.MAllViewsSizeIsShow;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener;
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager;

/*
 * 搜索酒店
 * */
public class SearchJuDian2VActivity<T> extends BaseActivity implements OkHttpUtil.OnDataListener, TextWatcher, AbsRecyclerViewAdapter.OnItemClickListener {


    @BindView(R.id.toolbar_searchBox)
    public AutoCompleteTextView autoTextView;
    @BindView(R.id.img_delete)
    ImageView imgDelete;
    @BindView(R.id.fl_history)
    TagFlowLayout flHistory;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.img_hot)
    ImageView imgHot;
    @BindView(R.id.fl_hot)
    TagFlowLayout flHot;
    @BindView(R.id.ll_hot)
    LinearLayout llHot;
    @BindView(R.id.img_view)
    ImageView imgView;
    @BindView(R.id.fl_view)
    TagFlowLayout flView;
    @BindView(R.id.ll_view)
    LinearLayout llView;
    @BindView(R.id.img_area)
    ImageView imgArea;
    @BindView(R.id.fl_area)
    TagFlowLayout flArea;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.img_shop)
    ImageView imgShop;
    @BindView(R.id.fl_shop)
    TagFlowLayout flShop;
    @BindView(R.id.ll_shop)
    LinearLayout llShop;
    @BindView(R.id.img_airport)
    ImageView imgAirport;
    @BindView(R.id.fl_airport)
    TagFlowLayout flAirport;
    @BindView(R.id.ll_airport)
    LinearLayout llAirport;
    @BindView(R.id.img_subway)
    ImageView imgSubway;
    @BindView(R.id.fl_subway)
    TagFlowLayout flSubway;
    @BindView(R.id.ll_subway)
    LinearLayout llSubway;
    @BindView(R.id.img_school)
    ImageView imgSchool;
    @BindView(R.id.fl_school)
    TagFlowLayout flSchool;
    @BindView(R.id.ll_school)
    LinearLayout llSchool;
    @BindView(R.id.img_hothouse)
    ImageView imgHothouse;
    /* @BindView(R.id.fl_hothouse)
     TagFlowLayout flHothouse;*/
    @BindView(R.id.ll_hothouse)
    LinearLayout llHothouse;
    //搜索时涉及控件
    @BindView(R.id.ll_head)
    LinearLayout llHead;


    @BindView(R.id.rv_search)
    RecyclerView rvSearch;

    @BindView(R.id.rv_hothouse)
    RecyclerView rv_hothouse;
    /*@BindView(R.id.nsv)
    NestedScrollView nsv;*/
    TextView tvFangyuan;

    @BindView(R.id.img_delete_search)
    ImageView imgDeleteSearch;

    @BindView(R.id.ll)
    LinearLayout ll;

    @BindView(R.id.ll_remenfangyuan)
    LinearLayout llRemenfangyuan;


    private String cityId;
    private LoadingDialog loadingDialog;

    private Gson gson = new GsonBuilder().create();
    private Search_filtersEntity search_filtersEntity;
    private Resources_filtersEntity resources_filtersEntity;
    private DbHUtils hDb;//房源
    private DbVUtils vDb;//目的地
    private SearchForJiuDian2VAdapter bournSearchAdapter;
    private TagAdapter<T> tagSearchAdapter;
    private List<H> hs;
    private DbBSHUtils bSHDb;
    private StatusLayoutManager statusLayoutManager;
    private Handler handler;
    private Runnable runnable;

    private SearchForMoreHotHouse2VAdapter searchForMoreHotHouse2VAdapter;
    private List<MinSuSearch.BodyEntity.HotelsEntity> housesSearchs = new ArrayList<>();
    private TagFlowLayout flSearchCity;
    private LinearLayout llSearchCity;
    private ImageView imgSearchCity;
    private MinSuSearch minSuSearch;
    private RecyclerView rvHead;
    private MinSuSearchHeadAdapter minSuSearchHeadAdapter;
    private LinearLayout llResult;
    private TextView tvSearch;
    private TextView tvSearchcurrentCity;
    private View inflate;
    private boolean mIsHomeFragment = false;


    @Override
    protected int getViewResId() {
        return R.layout.activity_search_minsu2;
    }

    @OnEditorAction(R.id.toolbar_searchBox)
    public boolean onEditAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            //ToastUtil.initToast(this, "hhhh");
            PhoneUtil.hideKeyboard(v);
            CharSequence text = v.getText();
            if (!TextUtils.isEmpty(text)) {
                bSHDb.insertOrReplace(text.toString(), System.currentTimeMillis());
               /* List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                setData(flHistory, llDelete, (List<T>) bournSearchhistories, false, null);*/
                Intent intent = new Intent(this, JiuDianSearchResultActivity.class);
                intent.putExtra("text", text.toString());
                if (mIsHomeFragment){
                    intent.putExtra("position", "home3vFragment");
                }
                startActivity(intent);

                finish();
            }


            return true;
        }
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
       /* if (bSHDb!=null&&flHistory!=null&&llDelete!=null){
            List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
            setData(flHistory, llDelete, (List<T>) bournSearchhistories, false, null);
        }*/

        // init();
    }

    @Override
    protected void loadData() {
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager(), "loading");
        HashMap<String, String> map = new HashMap<>();
        map.put("type", 4 + "");//1：民宿搜索，2：别墅搜索

        OkHttpUtil.getJSON(this, Constant.URL.modules_search_configurations, map, this);
        /*if (SharedPreferencesUtil.getHomeResTimeIsOver(this)) {
            OkHttpUtil.getJSON(this, Constant.URL.resources_filters, this);
        } else {
            initHouseRes(SharedPreferencesUtil.getHomeRes(this));
        }
*/

    }

    @Override
    protected void init() {
        mIsHomeFragment = getIntent().getBooleanExtra("home3vFragment", false);

        bournSearchAdapter = new SearchForJiuDian2VAdapter(this, rvSearch);
        inflate = View.inflate(this, R.layout.minsu_search_head, null);


        tvFangyuan = inflate.findViewById(R.id.tv_fangyuan);
        //城市
        flSearchCity = inflate.findViewById(R.id.fl_searchCity);

        llSearchCity = inflate.findViewById(R.id.ll_searchCity);
        imgSearchCity = inflate.findViewById(R.id.img_searchcCity);

        llResult = inflate.findViewById(R.id.ll_result);
        tvSearch = inflate.findViewById(R.id.tv_search);
        tvSearchcurrentCity = inflate.findViewById(R.id.tv_SearchcurrentCity);
        tvFangyuan.setText("酒店");
        //imgSearchCity = inflate.findViewById(R.id.img_searchcCity);
        imgSearchCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgSearchCity.setSelected(!imgSearchCity.isSelected());
                if (imgSearchCity.isSelected()) {
                    setViewHight(flSearchCity, true);
                } else {
                    setViewHight(flSearchCity, false);

                }
            }
        });
        rvHead = inflate.findViewById(R.id.rv_head);
        rvHead.setLayoutManager(new LinearLayoutManager(this));
        minSuSearchHeadAdapter = new MinSuSearchHeadAdapter(this);
        rvHead.setAdapter(minSuSearchHeadAdapter);


        statusLayoutManager = setupDefaultStatusLayoutManager(ll, new OnStatusChildClickListener() {
            @Override
            public void onEmptyChildClick(View view) {

            }

            @Override
            public void onErrorChildClick(View view) {
                loadingDialog = LoadingDialog.newInstance("加载中");
                loadingDialog.show(getFragmentManager(), "loading");

                loadData();
            }

            @Override
            public void onCustomerChildClick(View view) {

            }
        });
        hDb = DbHUtils.getInstance();
        vDb = DbVUtils.getInstance();
        bSHDb = DbBSHUtils.getInstance();
        cityId = getIntent().getStringExtra("cityId");


        autoTextView.addTextChangedListener(this);
        rvSearch.setLayoutManager(new LinearLayoutManager(this));
        rvSearch.setNestedScrollingEnabled(false);


        bournSearchAdapter.addHeaderView(inflate);
        rvSearch.setAdapter(bournSearchAdapter);

        rvSearch.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 1), getResources().getColor(R.color.colorBg), PhoneUtil.dp2px(this, 0)));
        bournSearchAdapter.setOnItemClickListener(this);
       /* View inflate = View.inflate(getContext(), R.layout.recycle_head, null);
        homeListSaleAdapter.addHeaderView(inflate);*/
        bournSearchAdapter.setEmptyView(R.layout.empty_search_tips_white);
        bournSearchAdapter.setEmptyTips(R.id.tv_emptyTips, "抱歉，未能搜索到相关内容");
        //bournSearchAdapter.setEmptyTips(R.id.tv_emptyTips, "");

        rvSearch.setNestedScrollingEnabled(false);

        List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
        setData(flHistory, llDelete, (List<T>) bournSearchhistories, true, null);

        //热门房源
        searchForMoreHotHouse2VAdapter = new SearchForMoreHotHouse2VAdapter(this, rv_hothouse);
        rv_hothouse.setLayoutManager(new LinearLayoutManager(this));
        rv_hothouse.setNestedScrollingEnabled(false);
        rv_hothouse.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 1), getResources().getColor(R.color.colorBgEEE), PhoneUtil.dp2px(this, 1)));

        rv_hothouse.setAdapter(searchForMoreHotHouse2VAdapter);

        searchForMoreHotHouse2VAdapter.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent = new Intent(SearchJuDian2VActivity.this, HouseDetailsActivityFor3.class);
                intent.putExtra("type", 0);
                intent.putExtra("houseId", search_filtersEntity.getBody().getHouses().get(position).getId());
                intent.putExtra("presellProduct", false);

                intent.putExtra("brokerAge", search_filtersEntity.getBody().getHouses().get(position).getCurrentPriceValue());
                intent.putExtra("persellId", search_filtersEntity.getBody().getHouses().get(position).getId());
                startActivity(intent);

            }
        });

        rv_hothouse.setNestedScrollingEnabled(false);

    }


    @Override
    public void onResponse(String url, String json) {
        if (statusLayoutManager != null) {

            statusLayoutManager.showSuccessLayout();
        }
        if (String.format(Constant.URL.modules_search_configurations, cityId).equals(url)) {
            /*if (!SharedPreferencesUtil.getHomeResTimeIsOver(this)) {
                dismissLoading();
            }*/
            dismissLoading();
            LogeUtil.e("modules_search_configurations", json);
            search_filtersEntity = gson.fromJson(json, Search_filtersEntity.class);
            if (search_filtersEntity != null && search_filtersEntity.getBody() != null && "200".equals(search_filtersEntity.getCode())) {

                initView();

            }

        } else if (Constant.URL.resources_filters.equals(url)) {
            LogeUtil.e("resources_filters", json);
            dismissLoading();
            SharedPreferencesUtil.saveHomeRes(this, System.currentTimeMillis(), json);
            initHouseRes(json);

        } else if (Constant.URL.BieShuSearch.equals(url)) {//别墅搜索
            LogeUtil.e("BieShuSearch", json);

            minSuSearch = gson.fromJson(json, MinSuSearch.class);
            if ("200".equals(minSuSearch.getCode())) {
                if (minSuSearch.getBody().getHotels() != null && minSuSearch.getBody().getHotels().size() > 0) {
                    tvFangyuan.setVisibility(View.VISIBLE);
                    housesSearchs = minSuSearch.getBody().getHotels();


                } else {
                    housesSearchs.clear();
                    tvFangyuan.setVisibility(View.GONE);

                }

                if (minSuSearch.getBody().getResult() == 2) {
                    llResult.setVisibility(View.VISIBLE);


                } else {
                    llResult.setVisibility(View.GONE);
                }


                if (minSuSearch.getBody().getCities() != null && minSuSearch.getBody().getCities().size() == 0 && minSuSearch.getBody().getGroups() != null && minSuSearch.getBody().getGroups().size() == 0 && housesSearchs.size() == 0) {
                    bournSearchAdapter.clearHeader();

                } else {
                    if (bournSearchAdapter.getHeaderCount() == 0) {
                        bournSearchAdapter.addHeaderView(inflate);
                    }
                }
                bournSearchAdapter.setData(housesSearchs);

                //setData(flSearch, llSearch, (List<T>) bieShuSearch.getBody().getVillages(), false);
                setData(flSearchCity, llSearchCity, (List<T>) minSuSearch.getBody().getCities(), false, imgSearchCity);
                minSuSearchHeadAdapter.setData(minSuSearch.getBody().getGroups());
            }
        }
    }

    private void initHouseRes(String json) {
        resources_filtersEntity = gson.fromJson(json, Resources_filtersEntity.class);
        if (resources_filtersEntity != null && resources_filtersEntity.getBody() != null && "200".equals(resources_filtersEntity.getCode())) {
            hDb.deleteAll();
            vDb.deleteAll();

            hDb.saveAll(resources_filtersEntity.getBody().getH());
            vDb.saveAll(resources_filtersEntity.getBody().getV());
            //   setViewHight(flSearch, false);

        }
    }

    private void initView() {
        final Search_filtersEntity.BodyEntity body = search_filtersEntity.getBody();
        //当地热门

        setData(flHot, llHot, (List<T>) body.getVillages(), true, imgHot);
        //观光景点
        setData(flView, llView, (List<T>) body.getScenic_spots(), true, imgView);

        //行政区
        setData(flArea, llArea, (List<T>) body.getAreas(), true, imgArea);
        //商圈
        setData(flShop, llShop, (List<T>) body.getBusiness(), true, imgShop);
        //机场 车站
        setData(flAirport, llAirport, (List<T>) body.getStation(), true, imgAirport);
        //地铁线路
        setData(flSubway, llSubway, (List<T>) body.getSubway_lines(), true, imgSubway);
        //高校
        setData(flSchool, llSchool, (List<T>) body.getColleges(), true, imgSchool);
        //热门房源
        if (body.getHouses() == null || body.getHouses().size() == 0) {
            llRemenfangyuan.setVisibility(View.GONE);
        }
        searchForMoreHotHouse2VAdapter.setData(body.getHouses());
        // setData(flHothouse, llHothouse, (List<T>) body.getHotels(), false);

    }

    private void setData(final TagFlowLayout fl, LinearLayout ll, final List<T> object, boolean isRetract, final ImageView imageView) {
        if (object == null || object.size() == 0) {
            ll.setVisibility(View.GONE);

        } else {
            ll.setVisibility(View.VISIBLE);

        }


        if (isRetract) {
            setViewHight(fl, false);
        } else {
            fl.specifyLines(999);


        }

        if (object != null) {
            // ll.setVisibility(View.VISIBLE);
            if (imageView != null) {
                //两行以下隐藏
                fl.setMAllViewsSizeIsShow(new MAllViewsSizeIsShow() {
                    @Override
                    public void setViewShow(boolean isShow) {
                        if (isShow) {
                            imageView.setVisibility(View.VISIBLE);
                        } else {
                            imageView.setVisibility(View.INVISIBLE);

                        }
                    }
                });
            }
            TagAdapter<T> tagAdapter = new TagAdapter<T>(object) {
                @Override
                public View getView(FlowLayout parent, int position, T s) {
                    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv_copynoselect,
                            fl, false);
                    if (s instanceof Search_filtersEntity.BodyEntity.VillagesEntity) {
                        //当地热门

                        Search_filtersEntity.BodyEntity.VillagesEntity hot = (Search_filtersEntity.BodyEntity.VillagesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.ScenicSpotsEntity) {
                        //观光景点
                        Search_filtersEntity.BodyEntity.ScenicSpotsEntity hot = (Search_filtersEntity.BodyEntity.ScenicSpotsEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.AreasEntity) {
                        //行政区
                        Search_filtersEntity.BodyEntity.AreasEntity hot = (Search_filtersEntity.BodyEntity.AreasEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.BusinessEntity) {
                        //商圈
                        Search_filtersEntity.BodyEntity.BusinessEntity hot = (Search_filtersEntity.BodyEntity.BusinessEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.StationEntity) {
                        //机场车站
                        Search_filtersEntity.BodyEntity.StationEntity hot = (Search_filtersEntity.BodyEntity.StationEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.SubwayLinesEntity) {
                        //地铁
                        Search_filtersEntity.BodyEntity.SubwayLinesEntity hot = (Search_filtersEntity.BodyEntity.SubwayLinesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.CollegesEntity) {
                        //高校
                        Search_filtersEntity.BodyEntity.CollegesEntity hot = (Search_filtersEntity.BodyEntity.CollegesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (s instanceof Search_filtersEntity.BodyEntity.HousesEntity) {
                        //热门房源
                        Search_filtersEntity.BodyEntity.HousesEntity hot = (Search_filtersEntity.BodyEntity.HousesEntity) s;
                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (object.get(0) instanceof BieShuSearch.BodyEntity.VillagesEntity) {
                        //搜索
                        BieShuSearch.BodyEntity.VillagesEntity hot = (BieShuSearch.BodyEntity.VillagesEntity) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (object.get(0) instanceof BournSearchhistory) {
                        //历史
                        BournSearchhistory hot = (BournSearchhistory) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (object.get(0) instanceof BieShuSearch.BodyEntity.CitiesEntity) {
                        //城市{别墅}
                        BieShuSearch.BodyEntity.CitiesEntity hot = (BieShuSearch.BodyEntity.CitiesEntity) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    } else if (object.get(0) instanceof MinSuSearch.BodyEntity.CitiesEntity) {
                        //城市{民宿}
                        MinSuSearch.BodyEntity.CitiesEntity hot = (MinSuSearch.BodyEntity.CitiesEntity) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    }


                    return tv;
                }
            };
            fl.setAdapter(tagAdapter);

            tagAdapter.notifyDataChanged();
            //行政区调别墅其他  都是民宿
            fl.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    if (!TextUtils.isEmpty(autoTextView.getText().toString())) {
                        bSHDb.insertOrReplace(autoTextView.getText().toString(), System.currentTimeMillis());
                        List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                        setData(flHistory, llDelete, (List<T>) bournSearchhistories, false, null);

                    }

                    if (object.get(0) instanceof Search_filtersEntity.BodyEntity.VillagesEntity) {
                        //当地热门
                        List<Search_filtersEntity.BodyEntity.VillagesEntity> hot = (List<Search_filtersEntity.BodyEntity.VillagesEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, BourbonListActivity.class);
                        //  intent.putExtra("villageId", hot.get(position).getId());
                        intent.putExtra("businessDistrictId", hot.get(position).getId());

                        // intent.putExtra("city", "");
                        intent.putExtra("area", hot.get(position).getName());
                        startActivity(intent);
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.ScenicSpotsEntity) {
                        //观光景点


                        List<Search_filtersEntity.BodyEntity.ScenicSpotsEntity> hot = (List<Search_filtersEntity.BodyEntity.ScenicSpotsEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        intent.putExtra("businessDistrictId", hot.get(position).getId());
                        //intent.putExtra("city", "");
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        intent.putExtra("area", hot.get(position).getName());


                        startActivity(intent);
                        finish();
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.AreasEntity) {
                        //行政区

                        List<Search_filtersEntity.BodyEntity.AreasEntity> hot = (List<Search_filtersEntity.BodyEntity.AreasEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        intent.putExtra("businessDistrictId", hot.get(position).getId());
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        intent.putExtra("area", hot.get(position).getName());
                        //intent.putExtra("areaId", hot.get(position).getId());


                        startActivity(intent);
                        finish();
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.BusinessEntity) {
                        //商圈


                        List<Search_filtersEntity.BodyEntity.BusinessEntity> hot = (List<Search_filtersEntity.BodyEntity.BusinessEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        intent.putExtra("businessDistrictId", hot.get(position).getId());
                        //intent.putExtra("city", "");
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        intent.putExtra("area", hot.get(position).getName());

                        startActivity(intent);
                        finish();
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.StationEntity) {
                        //机场车站


                        List<Search_filtersEntity.BodyEntity.StationEntity> hot = (List<Search_filtersEntity.BodyEntity.StationEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        intent.putExtra("businessDistrictId", hot.get(position).getId());
                        //intent.putExtra("city", "");
                        intent.putExtra("area", hot.get(position).getName());
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        startActivity(intent);
                        finish();
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.SubwayLinesEntity) {
                        //地铁

                        List<Search_filtersEntity.BodyEntity.SubwayLinesEntity> hot = (List<Search_filtersEntity.BodyEntity.SubwayLinesEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        intent.putExtra("businessDistrictId", hot.get(position).getId());
                        //intent.putExtra("city", "");
                        intent.putExtra("area", hot.get(position).getName());
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        startActivity(intent);
                        finish();
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.CollegesEntity) {
                        //高校
                        List<Search_filtersEntity.BodyEntity.CollegesEntity> hot = (List<Search_filtersEntity.BodyEntity.CollegesEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        intent.putExtra("businessDistrictId", hot.get(position).getId());
                        //intent.putExtra("city", "");
                        intent.putExtra("area", hot.get(position).getName());
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        startActivity(intent);
                        finish();
                    } else if (object.get(0) instanceof Search_filtersEntity.BodyEntity.HousesEntity) {
                        //热门房源
                        List<Search_filtersEntity.BodyEntity.HousesEntity> hot = (List<Search_filtersEntity.BodyEntity.HousesEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, HouseDetailsActivityFor3.class);
                        intent.putExtra("type", 0);
                        intent.putExtra("houseId", hot.get(position).getId());
                        intent.putExtra("presellProduct", hot.get(position).getHouseType() != 0);

                        intent.putExtra("brokerAge", hot.get(position).getBrokerAge());
                        intent.putExtra("persellId", hot.get(position).getProductId());
                        startActivity(intent);
                    } else if (object.get(0) instanceof BieShuSearch.BodyEntity.VillagesEntity) {//搜索
                        List<BieShuSearch.BodyEntity.VillagesEntity> hot = (List<BieShuSearch.BodyEntity.VillagesEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, BourbonListActivity.class);
                        intent.putExtra("villageId", hot.get(position).getId());
                        intent.putExtra("city", "");
                        intent.putExtra("area", "");
                        startActivity(intent);
                    } else if (object.get(0) instanceof BournSearchhistory) {//历史
                        List<BournSearchhistory> hot = (List<BournSearchhistory>) object;
                        autoTextView.setText(hot.get(position).getName());
                        Editable etext = autoTextView.getText();
                        Selection.setSelection(etext, etext.length());
                    } else if (object.get(0) instanceof BieShuSearch.BodyEntity.CitiesEntity) {
                        //城市{别墅}

                      /*  List<BieShuSearch.BodyEntity.CitiesEntity> hot = (List<BieShuSearch.BodyEntity.CitiesEntity>) object;
                        Intent intent = new Intent(SearchMinSu2VActivity.this, JiuDianSearchResultActivity.class);
                        AppContext.cityCurrent=hot.get(position).getName();
                        AppContext.currentCityidCurrent=hot.get(position).getId();
                         
                        startActivity(intent);*/
                    } else if (object.get(0) instanceof MinSuSearch.BodyEntity.CitiesEntity) {
                        //城市{民宿}

                        List<MinSuSearch.BodyEntity.CitiesEntity> hot = (List<MinSuSearch.BodyEntity.CitiesEntity>) object;
                        Intent intent = new Intent(SearchJuDian2VActivity.this, JiuDianSearchResultActivity.class);
                        AppContext.cityCurrent = hot.get(position).getName();
                        AppContext.currentCityidCurrent = hot.get(position).getId();
                        if (mIsHomeFragment){
                            intent.putExtra("position", "home3vFragment");
                        }
                        startActivity(intent);
                        finish();
                    }

                    return true;
                }
            });
        } else {
            //ll.setVisibility(View.GONE);

        }


    }

    private void setDataForSearch(final TagFlowLayout fl, LinearLayout ll, final List<T> object, boolean isRetract) {

    }

    private void setViewHight(TagFlowLayout view, boolean isMatch) {
        if (isMatch) {
            view.specifyLines(9999);
            view.getAdapter().notifyDataChanged();
        } else {
            view.specifyLines(2);
        }
    }

    @Override
    public void onFailure(String url, String error) {
        dismissLoading();
        if (statusLayoutManager != null) {

            statusLayoutManager.showErrorLayout();
        }
        ToastUtil.initToast(this, "网络错误");
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        /*if (srl != null && srl.isRefreshing()) {
            srl.setRefreshing(false);
        }*/

    }


    @OnClick({R.id.img_delete_search, R.id.tv_cancle, R.id.img_delete, R.id.img_hot, R.id.img_view, R.id.img_area, R.id.img_shop, R.id.img_airport, R.id.img_subway, R.id.img_school, R.id.img_hothouse})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_delete_search://搜索置空
                autoTextView.setText("");
                imgDeleteSearch.setVisibility(View.INVISIBLE);
                break;
            case R.id.tv_cancle:
                onBackPressed();
                break;
            case R.id.img_delete:
                bSHDb.deleteAll();
                List<BournSearchhistory> bournSearchhistories = bSHDb.queryDownAllOrderByDate();
                setData(flHistory, llDelete, (List<T>) bournSearchhistories, false, null);
                break;
            case R.id.img_search:
                /*imgSearch.setSelected(!imgSearch.isSelected());
                if (imgSearch.isSelected()) {
                    setViewHight(flSearch, true);
                } else {
                    setViewHight(flSearch, false);

                }*/


                break;
            case R.id.img_hot:
                imgHot.setSelected(!imgHot.isSelected());
                if (imgHot.isSelected()) {
                    setViewHight(flHot, true);
                } else {
                    setViewHight(flHot, false);

                }


                break;
            case R.id.img_view:
                imgView.setSelected(!imgView.isSelected());
                if (imgView.isSelected()) {
                    setViewHight(flView, true);
                } else {
                    setViewHight(flView, false);

                }
                break;
            case R.id.img_area:
                imgArea.setSelected(!imgArea.isSelected());
                if (imgArea.isSelected()) {
                    setViewHight(flArea, true);
                } else {
                    setViewHight(flArea, false);

                }
                break;
            case R.id.img_shop:
                imgShop.setSelected(!imgShop.isSelected());
                if (imgShop.isSelected()) {
                    setViewHight(flShop, true);
                } else {
                    setViewHight(flShop, false);

                }
                break;
            case R.id.img_airport:
                imgAirport.setSelected(!imgAirport.isSelected());
                if (imgAirport.isSelected()) {
                    setViewHight(flAirport, true);
                } else {
                    setViewHight(flAirport, false);

                }
                break;
            case R.id.img_subway:
                imgSubway.setSelected(!imgSubway.isSelected());
                if (imgSubway.isSelected()) {
                    setViewHight(flSubway, true);
                } else {
                    setViewHight(flSubway, false);

                }
                break;
            case R.id.img_school:
                imgSchool.setSelected(!imgSchool.isSelected());
                if (imgSchool.isSelected()) {
                    setViewHight(flSchool, true);
                } else {
                    setViewHight(flSchool, false);

                }
                break;
            case R.id.img_hothouse:
              /*  imgHothouse.setSelected(!imgHothouse.isSelected());
                if (imgHothouse.isSelected()) {
                    setViewHight(flHothouse, true);
                } else {
                    setViewHight(flHothouse, false);

                }*/
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(final CharSequence s, int start, int before, int count) {
        if (handler != null && runnable != null) {
            handler.removeCallbacks(runnable);
        }
        if (!TextUtils.isEmpty(s)) {
            tvSearch.setText("当前城市未找到" + "“" + s + "“");
            tvSearchcurrentCity.setText("“" + AppContext.cityCurrent + "“" + "城市以外的查询结果");
        }


        handler = new Handler();
        //当地热门
        runnable = new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(s)) {
                    llHead.setVisibility(View.VISIBLE);
                    rvSearch.setVisibility(View.GONE);
                    tvFangyuan.setVisibility(View.GONE);
                    imgDeleteSearch.setVisibility(View.INVISIBLE);

                } else {
                    llHead.setVisibility(View.GONE);
                    rvSearch.setVisibility(View.VISIBLE);
                    imgDeleteSearch.setVisibility(View.VISIBLE);

                }

                if (!TextUtils.isEmpty(s)) {
                    bournSearchAdapter.setSearchData(s.toString());


                    //bournSearchAdapter.setData(hs);
                        /*
                        * List<com.villaday.idf.model.H> hs1=new ArrayList<>();

                        hs1.add(hs.get(0));
                        bournSearchAdapter.setData(hs1);

                        List<T> vs1 = (List<T>) vDb.queryWithSomethingAll(s.toString());
                        vs1.add(vs.get(0));
                        setData(flSearch, llSearch, vs1, false)
                        * */
                    //酒店搜索
                    HashMap<String, String> map = new HashMap<>();
                    map.put("keyword", s.toString());
                    map.put("type", "4");
                    OkHttpUtil.getJSON(SearchJuDian2VActivity.this, Constant.URL.BieShuSearch, map, SearchJuDian2VActivity.this);


                }

            }

        };
        handler.post(runnable);


    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onItemClick(View v, int position) {


        Intent intent = new Intent(this, WineShopDetailsActivity.class);

        intent.putExtra("hotelId", housesSearchs.get(position).getId());
        startActivity(intent);



      /*  Intent intent = new Intent(SearchJuDian2VActivity.this, HouseDetailsActivityFor3.class);
        intent.putExtra("type", 0);
        intent.putExtra("houseId", minSuSearch.getBody().getHotels().get(position).getId());
        intent.putExtra("presellProduct", false);

        intent.putExtra("brokerAge", minSuSearch.getBody().getHotels().get(position).getCurrentPriceValue());
        intent.putExtra("persellId", minSuSearch.getBody().getHotels().get(position).getId());
        startActivity(intent);*/


    }


}
